﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FWMS
{
    public partial class Form_cailiaoguanli : Form
    {
        public Form_cailiaoguanli()
        {
            InitializeComponent();
        }
        //数据库中数据的总行数
        private static int resultrows;
        //查询结果的数据
        public object[,] Selectresult;
        //条件查询结果的数据
        public object[,] ConditionSelectresult;
        //最大ID值
        private static int maxid;
        //最大ID值
        private static string selectid;
        //双击提取材料名
        private static string selectcailiaoname;
        private void Form_cailiaoguanli_Load(object sender, EventArgs e)
        {
            //this.skinEngine1.SkinFile = "OneBlue.ssk";
            this.tb_cailiaoname.Focus();
            this.comboBox1.Items.Add("");
            this.comboBox1.Items.Add("原料");
            this.comboBox1.Items.Add("调味料");
            this.comboBox1.Items.Add("辅料");
            this.bt_cailiaoguanli_modify.Enabled = false;
            selectall();
        }
        //材料管理添加数据
        private void bt_cailiaoguanli_add_Click(object sender, EventArgs e)
        {
            Service.CailiaoguanliService cailiaoguanliservice = new FWMS.Service.CailiaoguanliService();
            Bean.CailiaoguanliBean bean = new FWMS.Bean.CailiaoguanliBean();
            if ("".Equals(this.tb_cailiaoname.Text.Trim()))
            {
                MessageBox.Show("材料名不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_cailiaoname.Focus();
                return;
            }
            else if (comboBox1.SelectedItem == null || "".Equals((string)comboBox1.SelectedItem))
            {
                MessageBox.Show("材料类型不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.comboBox1.Focus(); 
                return;
            }
            else
            {
                if (COMMAND.Command.bianli(Selectresult, this.tb_cailiaoname.Text, maxid - 1))
                {
                    bean.Cailiaoname = this.tb_cailiaoname.Text;
                    bean.Cailiaotype = (string)this.comboBox1.SelectedItem;
                    bean.Resultrow = maxid;
                    if (cailiaoguanliservice.cailiaoguanliServiceAdd(bean))
                    {
                        selectall();
                        foreach (DataGridViewRow row in this.dataGridView1.Rows)
                        {
                            if (bean.Cailiaoname.Equals(row.Cells[1].Value))
                            {
                               this.dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
                                
                                row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                                break;
                            }
                        }
                        MessageBox.Show("材料添加成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                    }
                    else
                    {
                        MessageBox.Show("材料添加失败，请重新添加！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.tb_cailiaoname.Focus();
                    }
                }
                else
                {
                     MessageBox.Show("材料添加失败，材料库中已存在该材料！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tb_cailiaoname.Focus();
                }

            }

            this.bt_cailiaoguanli_modify.Enabled = false;
        }
        //结果集双击事件
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            selectcailiaoname = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 1].ToString().Trim();
            selectid = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 0];
            this.tb_cailiaoname.Text = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 1];
            this.comboBox1.SelectedItem = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 2];
            this.bt_cailiaoguanli_modify.Enabled = true;
       }
        //材料管理修改事件
        private void bt_cailiaoguanli_modify_Click(object sender, EventArgs e)
        {
            if ("".Equals(this.tb_cailiaoname.Text.Trim()))
            {
                MessageBox.Show("材料名不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_cailiaoname.Focus();
                return;
            }
            else if (comboBox1.SelectedItem == null || "".Equals((string)comboBox1.SelectedItem))
            {
                MessageBox.Show("材料类型不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.comboBox1.Focus();
                return;
            }
            if (!selectcailiaoname.Equals(this.tb_cailiaoname.Text.Trim()))
            {
                if (!COMMAND.Command.bianli(Selectresult, this.tb_cailiaoname.Text.Trim(), maxid - 1))
                {
                    MessageBox.Show("材料修改失败，材料库中存在同名材料！", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tb_cailiaoname.Focus();
                    return;
                }
            }
            Service.CailiaoguanliService cailiaoguanliserivce = new FWMS.Service.CailiaoguanliService();
            Bean.CailiaoguanliBean bean = new FWMS.Bean.CailiaoguanliBean();
            bean.Id = selectid;
            bean.Cailiaoname = this.tb_cailiaoname.Text;
            bean.Cailiaotype = (string)this.comboBox1.SelectedItem;

            if (cailiaoguanliserivce.cailiaoguanliServiceModify(bean))
            {
                selectall();
                foreach(DataGridViewRow row in this.dataGridView1.Rows )
                {
                    if(bean.Cailiaoname.Equals(row.Cells[1].Value))
                    {
                        if (row.Index > 12)
                        {
                            this.dataGridView1.FirstDisplayedScrollingRowIndex = row.Index-3;
                        }
                        row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        break;
                    }
                }
                MessageBox.Show("材料修改成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("材料修改失败,请重新修改！", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tb_cailiaoname.Focus();
            }
        }
        //查询事件
        private void bt_cailiaoguanli_select_Click(object sender, EventArgs e)
        {
            //任何条件都不加 查询全部
            if ("".Equals(this.tb_cailiaoname.Text.Trim()) && (comboBox1.SelectedItem == null || "".Equals((string)comboBox1.SelectedItem)))
            {
                //材料名和材料类型都不输入，提示是否查询所有数据
                if (MessageBox.Show("您没有输入查询条件，默认会查询出所有材料，您要查询所有材料么？", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    selectall();
                }
                this.tb_cailiaoname.Focus();
            }
            else
            {
                Service.CailiaoguanliService cailiaoguanliservice = new FWMS.Service.CailiaoguanliService();
                Bean.CailiaoguanliBean bean = new FWMS.Bean.CailiaoguanliBean();
                bean.Cailiaoname = this.tb_cailiaoname.Text.Trim();
                bean.Cailiaotype = (string)this.comboBox1.SelectedItem;
                bean = cailiaoguanliservice.cailiaoguanliServiceSelectContation(bean);
                if (bean.Selectresult == null)
                {
                    MessageBox.Show("材料库中没有当前材料！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.tb_cailiaoname.Focus();
                }
                else
                {
                    resultrows = bean.Resultrow;
                    ConditionSelectresult = bean.Selectresult;
                    datagridviewadddata(bean);
                }
            }
            this.bt_cailiaoguanli_modify.Enabled = false;
        }
        //接收数据并显示
        private void datagridviewadddata(Bean.CailiaoguanliBean bean)
        {
            this.dataGridView1.Rows.Clear();
            this.label_selectresult.Text = "查询结果：" + bean.Resultrow.ToString();
            for (int j = 0; j < bean.Resultrow; j++)
            {
                this.dataGridView1.Rows.Add(bean.Selectresult[j, 0], bean.Selectresult[j, 1], bean.Selectresult[j, 2]);
            }
        }
        //查询全部数据
        private void selectall()
        {
            Service.CailiaoguanliService cailiaoguanliservice = new FWMS.Service.CailiaoguanliService();
            Bean.CailiaoguanliBean bean = new FWMS.Bean.CailiaoguanliBean();
            bean = cailiaoguanliservice.cailiaoguanliServiceSelect();
            resultrows = bean.Resultrow;
            maxid = bean.Resultrow + 1;
            Selectresult = bean.Selectresult;
            ConditionSelectresult = bean.Selectresult;
            if (bean.Selectresult == null)
            {
                MessageBox.Show("材料库中没有任何材料！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.tb_cailiaoname.Focus();
            }
            else
            {
                datagridviewadddata(bean);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
